<template>
  <div class="site-wrapper site-page--login">
    <div class="site-content__wrapper">
      <div class="login-main">
        <div class="login-header"><img src="../../assets/img/login_header.png" alt=""></div>
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                 status-icon class="input-form">
          <el-form-item prop="userName" class="input-item">
            <el-input v-model="dataForm.userName" placeholder="手机号" size="large"></el-input>
          </el-form-item>
          <el-form-item prop="password" class="input-item">
            <el-input v-model="dataForm.password" type="password" placeholder="密码" size="large"></el-input>
          </el-form-item>
          <el-form-item prop="captcha" class="input-item">
            <el-input v-model="dataForm.captcha" placeholder="验证码" class="input-code" size="large">
            </el-input>
            <img :src="captchaPath" @click="getCaptcha()" alt="" class="code">
          </el-form-item>
          <el-form-item class="input-item submit">
            <el-button type="warning" @click="dataFormSubmit()">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  import {getUUID} from '@/utils'

  export default {
    data() {
      return {
        dataForm: {
          userName: '',
          password: '',
          uuid: '',
          captcha: ''
        },
        dataRule: {
          userName: [
            {required: true, message: '帐号不能为空', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '密码不能为空', trigger: 'blur'}
          ],
          captcha: [
            {required: true, message: '验证码不能为空', trigger: 'blur'}
          ]
        },
        captchaPath: ''
      }
    },
    created() {
      this.getCaptcha()
    },
    methods: {
      // 提交表单
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/sys/login'),
              method: 'post',
              data: this.$http.adornData({
                'username': this.dataForm.userName,
                'password': this.dataForm.password,
                'uuid': this.dataForm.uuid,
                'captcha': this.dataForm.captcha
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$cookie.set('token', data.token)
                this.$router.replace({name: 'home'})
              } else {
                this.getCaptcha()
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取验证码
      getCaptcha() {
        this.dataForm.uuid = getUUID()
        this.captchaPath = this.$http.adornUrl(`/captcha.jpg?uuid=${this.dataForm.uuid}`)
      }
    }
  }
</script>

<style lang="scss">
  .site-wrapper.site-page--login {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    background-color: #f0edec;
    left: 0;
    overflow: hidden;
    &:before {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      content: "";
      background-size: 50%;
      background: url(~@/assets/img/login_bg.png) no-repeat;
    }
    .site-content__wrapper {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      padding: 0;
      margin: 0;
      overflow-x: hidden;
      overflow-y: auto;
      background-color: transparent;
    }

    .el-input__inner {
      border-left: none;
      border-right: none;
      border-top: none;
      border-radius: 0;
      padding: 0;
      color: #666666;
    }

    .input-item {
      width: 320px;
      margin: 20px 40px;
    }
    .input-item.submit {
      margin-top: 50px;
      margin-bottom: 40px;
    }
    .input-item.submit button {
      width: 100%;
    }
    .input-code {
      width: 170px;
      margin-right: 10px;
    }
    div.el-form-item.input-item.submit.el-form-item--feedback.el-form-item--medium > div > button {
      background: linear-gradient(#fbab61, #f9760a);
      border-radius: 18px;
      border-color: transparent;
    }
    div.el-form-item.input-item.submit.el-form-item--feedback.el-form-item--medium > div > button:focus,
    div.el-form-item.input-item.submit.el-form-item--feedback.el-form-item--medium > div > button:hover {
      background: linear-gradient(#fbab61, #ff910a);
      border-color: transparent;
    }
    .el-form-item.is-success .el-input__inner {
      border-color: #f9760a;
    }
    .el-form-item.is-success .el-input__validateIcon {
      color: #f9760a;
    }
    .el-input__inner:focus {
      border-color: #f9760a;
    }
    .code {
      height: 32px;
      margin-top: -6px;
    }
    .login-main {
      position: absolute;
      top: 50%;
      right: 0;
      margin-right: 200px;
      margin-top: -208px;
      border-radius: 2px;
      display: inline-block;
      background-color: white;
    }
    .login-header {
      text-align: center;
    }
    .login-header img {
      margin: 40px 0 20px;
      height: 64px;
    }
  }
</style>
